using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using csharp.rad.general;
using System.IO;
using WebCam_Capture;
using ComponentFactory.Krypton.Toolkit;

namespace csharp.rad.sisponto
{
    public partial class frmTeste : KryptonForm
    {
        private Database bd;
        Database.Result dbResult = new Database.Result();
        private WebCam_Capture.WebCamCapture WebCamCapture;

        public frmTeste(Database myBase)
        {
            InitializeComponent();
            this.bd = myBase;
        }

        private void btnMostraFoto_Click(object sender, EventArgs e)
        {
            string _sql,num;

            num = numericUpDown1.Value.ToString();
            System.Data.DataSet dtsDados= new DataSet();
            _sql = "SELECT foto INTO DUMPFILE 'C:/teste/retorno"+num+".jpg' FROM funcionario WHERE id="+num+";";

            dtsDados = bd.ExecuteQuery(_sql, "funcionario",out dbResult);
            if (dbResult.Status == Database.Status.Failed)
                KryptonMessageBox.Show("Erro!!!! hahahahha!");
            else
            {
                pictureBox1.Image = System.Drawing.Image.FromFile("C:\\teste\\retorno"+num+".jpg");
            }
        }

        private void WebCamCapture_ImageCaptured(object source, WebCam_Capture.WebcamEventArgs e)
        {
            // set the picturebox picture
            this.pictureBox2.Image = e.WebCamImage;
        }

        private void frmTeste_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.WebCamCapture.Stop();
           /* string[] arquivos = Directory.GetFiles("C:\\teste");

            pictureBox1.Dispose();
            foreach(string file in arquivos)
               File.Delete(Path.Combine("C:\\teste",file));*/
        }

        private void frmTeste_Load(object sender, EventArgs e)
        {
            this.WebCamCapture.CaptureHeight =200;
            this.WebCamCapture.CaptureWidth =  200;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.WebCamCapture.TimeToCapture_milliseconds = 20;
            this.WebCamCapture.Start(0);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.WebCamCapture.Stop();
            button3.Enabled = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (pictureBox2.IsAccessible)
            {
                pictureBox2.Image.Save("C:\\teste.jpg");
                button3.Enabled = false;
            }
            else
                KryptonMessageBox.Show("Capture a imagem antes de salvar");
        }

       private void button4_Click(object sender, EventArgs e)
       {
          string imagem ;
          
          imagem = Application.StartupPath+"\\temp.jpg";
          if( File.Exists(imagem))
            File.Delete(imagem);
           
          pictureBox2.Image.Save(imagem);
          FileStream fsFile = new FileStream(imagem, FileMode.Open, FileAccess.Read);
          long tam = fsFile.Length;
          byte [] btImage = new byte[tam];
          fsFile.Read(btImage,0,btImage.Length);
          fsFile.Close();
          File.Delete(imagem);
          
          fsFile = File.Open("C:\\imagem.txt",FileMode.Create,FileAccess.Write);
          fsFile.Write(btImage,0,btImage.Length);
          fsFile.Close();
       }

       private void button5_Click(object sender, EventArgs e)
       {
          FileStream fsFile = new FileStream("C:\\imagem.txt",FileMode.Open,FileAccess.Read);
          byte[] btImage = new byte[fsFile.Length];
          fsFile.Read(btImage,0, Convert.ToInt32(fsFile.Length));
			 fsFile.Close();
          MemoryStream msImage = new MemoryStream(btImage);
          pictureBox2.Image = Image.FromStream(msImage);
       }

		 private void button6_Click(object sender, EventArgs e)
		 {
			 //FileStream fsFile = new FileStream("C:\\imagem.txt", FileMode.Open, FileAccess.Read);
			 MemoryStream msT = new MemoryStream();
			 Image img = pictureBox2.Image;
			 img.Save(msT, System.Drawing.Imaging.ImageFormat.Jpeg);
			 byte[] btImage = msT.ToArray();
			 //byte[] btImage = new byte[fsFile.Length];
			 //fsFile.Read(btImage, 0, Convert.ToInt32(fsFile.Length));
			 //fsFile.Close();
			 //string imgStr = "0x" + BitConverter.ToString(btImage).Replace("-",string.Empty);
			 string imgStr = Convert.ToBase64String(btImage);

			 byte[] teste = new byte[imgStr.Length];
			 teste = Convert.FromBase64String(imgStr);
			 MemoryStream ms = new MemoryStream(teste);
			 pictureBox1.Image = Image.FromStream(ms);
			 //fsFile.Close();
		 }

        
        void Button7Click(object sender, EventArgs e)
        {
        	String temp;
        	
        	temp = maskedTextBox1.Text;
        	temp = maskedTextBox1.Mask;
        	
        	bool tmp = maskedTextBox1.MaskCompleted;
        	tmp = maskedTextBox1.MaskFull;
        }
	 }
}
